package server.tool;
import server.config.exception.BizException;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @description:
 * @author: 许哲
 * @createTime: 2021/11/22 16:38
 * @version: 1.0
 */
public class Main {
    private static final String CONTENT_TYPE = "multipart/form-data"; //内容类型
    private static final String BOUNDARY = "FlPm4LpSXsE"; //定义数据分隔符(可以随意更改)

    public static void main(String[] args) throws IOException {
       /* Map<String,String> mp = new HashMap<>();
        mp.put("scene","pc");
        mp.put("risk_info","{\"user_agent\":\"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36\"}");
        mp.put("process","{\"type\":\"cache\",\"data\":\"960b4d5715b426a4ce53e4453b296bf217fe06a8708206149f67ef6edb32a2bb\"}");
        mp.put("biz_content","{\"trade_no\":\"SP2021112216240605831931240131\"}");
        http("https://tp-pay.snssdk.com/gateway-cashier/tp.cashier.trade_query",mp);*/
        Map<String, String> mp = new HashMap<>();
        mp.put("scene", "pc");
        mp.put("risk_info", "{\"user_agent\":\"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36\"}");
        mp.put("biz_content", "{\"params\":{\"app_id\":\"800061972401\",\"exts\":\"{\\\"qrcode_validity_seconds\\\":60}\",\"merchant_id\":\"1200006197\",\"out_order_no\":\"10000017033366261027312672\",\"return_scheme\":\"\",\"return_url\":\"https://www.douyin.com/pay/union_pay/\",\"sign\":\"bdd428862a40aa9852e6ff8124937d9f\",\"sign_type\":\"MD5\",\"switch\":\"00\",\"timestamp\":\"1637583224\",\"total_amount\":\"100\",\"trade_no\":\"SP2021112220133100223688013029\",\"trade_type\":\"H5\",\"uid\":\"103395765779\"}}");
        http("https://tp-pay.snssdk.com/gateway-cashier/tp.cashier.trade_create", mp);
    }

    public static void get() {
        String path = "https://tp-pay.snssdk.com/gateway-cashier/tp.cashier.trade_create";
        URL url = null;
        HttpURLConnection con = null;
        try {
            url = new URL(path);
            con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");
            int rom = new Random().nextInt(new ClientAgent().getUA().size());
            con.setRequestProperty("User-Agent", new ClientAgent().getUA().get(rom));
            con.setRequestProperty("Cookie", "__ac_signature=_02B4Z6wo00f013o9UoQAAIDAsoGbmzWuuBN6HVYAAL8o3b;ttcid=87fe4974730748ff9456878fe9d9ce1b24;ttwid=1%7CJZuIuxryuaRqRxKVdDBNPgmvQijlmGd8EL2OeUznWbs%7C1637062666%7C1da83b2653bd07da29e06ed6862662d464c2ac43929b4f79dce150a970f0d364;_tea_utm_cache_6383=undefined;passport_csrf_token_default=2da6547312e042653fe5f9bfa68707bc;passport_csrf_token=2da6547312e042653fe5f9bfa68707bc;_tea_utm_cache_1300=undefined;_tea_utm_cache_2018=undefined;MONITOR_DEVICE_ID=717b3003-29b8-429d-b231-ddd13abedb5e;sso_auth_status=a7145009ad79a7bdca47db542f17cc99;sso_auth_status_ss=a7145009ad79a7bdca47db542f17cc99;d_ticket=62cff87f3ff289aa7437ccc909334e8ef4ee4;FOLLOW_NUMBER_YELLOW_POINT_INFO=MS4wLjABAAAAKPzirJw1HGVfEwO4NXF6n_SBe0Og9u-bYKFVxzFVHQg%2F1637078400000%2F0%2F1637062705917%2F0;s_v_web_id=verify_kw285zal_5fTNFn8C_S8jN_46E0_AYRu_i7IsGrrtPUjH;MONITOR_WEB_ID=a3f7ce26-6716-4417-8c1b-1733feea0a5b;n_mh=q5_EQDE9aAOu8snC-S8tkXI4H3hDknhfcmYjhD6ViYE;sso_uid_tt=787bc71160e377bf6343e82b768a4f78;sso_uid_tt_ss=787bc71160e377bf6343e82b768a4f78;toutiao_sso_user=fcae792d33a9ac25fcfea9209f2b1629;toutiao_sso_user_ss=fcae792d33a9ac25fcfea9209f2b1629;odin_tt=2439239b99d12ac4e96f073a38189b203807151bf36064976df69cc32b94335a0eca24c3487da8ad1f2ca314cae2481fdcd3129ed328467786b775ec061f49b2;passport_auth_status_ss=521e8181083ccca5f4f5f8d644428e5e%2C27cb9a0918e31b0f2c171e63a68c6f46;sid_guard=9cc14310155f63154c19bf56377482ea%7C1637508768%7C5184000%7CThu%2C+20-Jan-2022+15%3A32%3A48+GMT;uid_tt=6f90575096c31a3263c775d0d76a33c4;uid_tt_ss=6f90575096c31a3263c775d0d76a33c4;sid_tt=9cc14310155f63154c19bf56377482ea;sessionid=9cc14310155f63154c19bf56377482ea;sessionid_ss=9cc14310155f63154c19bf56377482ea;sid_ucp_v1=1.0.0-KDBkNzlmNzA5NmRmOWUwNWNiODM3MTg4ZWFjMDQ5NTM4OTliMmZhNDgKFQiT3PiWgQMQoM3pjAYYlk44BkD0BxoCbHEiIDljYzE0MzEwMTU1ZjYzMTU0YzE5YmY1NjM3NzQ4MmVh;ssid_ucp_v1=1.0.0-KDBkNzlmNzA5NmRmOWUwNWNiODM3MTg4ZWFjMDQ5NTM4OTliMmZhNDgKFQiT3PiWgQMQoM3pjAYYlk44BkD0BxoCbHEiIDljYzE0MzEwMTU1ZjYzMTU0YzE5YmY1NjM3NzQ4MmVh;passport_auth_status=521e8181083ccca5f4f5f8d644428e5e%2C27cb9a0918e31b0f2c171e63a68c6f46;msToken=07kqR5HDcD2pr9bn_ryTmUsRhlxe-hPZOcyPgdGgdq26WdibS9fpfW0dGvwJLHZk1_BeZVHuk9EnBfQzWsMUvu7Ije5lAbSqyGv4Sgqgmo22EllBONC8zA==; ");
            con.connect();
            if (con.getResponseCode() == 200) {
                BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String line;
                StringBuilder sb = new StringBuilder();
                while ((line = br.readLine()) != null) {// 循环读取流
                    sb.append(line);
                }
                br.close();// 关闭流
                System.out.println(sb.toString());
            } else {
                throw new BizException("支付失败!");
            }
        } catch (Exception e) {
            throw new BizException("支付失败!");
        } finally {
            con.disconnect();
        }
    }

    public static String http(String url, Map<String, String> params) {
        URL u = null;
        HttpURLConnection con = null;
        // 构建请求参数
        StringBuffer sb = new StringBuffer();
        if (params != null) {
            for (Map.Entry<String, String> e : params.entrySet()) {
                sb.append(e.getKey());
                sb.append("=");
                sb.append(e.getValue());
                sb.append("&");
            }
            sb.substring(0, sb.length() - 1);
        }
        System.out.println("send_url:" + url);
        System.out.println("send_data:" + sb.toString());
        // 尝试发送请求
        try {
            u = new URL(url);
            con = (HttpURLConnection) u.openConnection();
            //// POST 只能为大写，严格限制，post会不识别
            con.setRequestMethod("POST");
            int rom = new Random().nextInt(new ClientAgent().getUA().size());
            con.setRequestProperty("User-Agent", new ClientAgent().getUA().get(rom));
            con.setRequestProperty("Cookie", "__ac_signature=_02B4Z6wo00f013o9UoQAAIDAsoGbmzWuuBN6HVYAAL8o3b;ttcid=87fe4974730748ff9456878fe9d9ce1b24;ttwid=1%7CJZuIuxryuaRqRxKVdDBNPgmvQijlmGd8EL2OeUznWbs%7C1637062666%7C1da83b2653bd07da29e06ed6862662d464c2ac43929b4f79dce150a970f0d364;_tea_utm_cache_6383=undefined;passport_csrf_token_default=2da6547312e042653fe5f9bfa68707bc;passport_csrf_token=2da6547312e042653fe5f9bfa68707bc;_tea_utm_cache_1300=undefined;_tea_utm_cache_2018=undefined;MONITOR_DEVICE_ID=717b3003-29b8-429d-b231-ddd13abedb5e;sso_auth_status=a7145009ad79a7bdca47db542f17cc99;sso_auth_status_ss=a7145009ad79a7bdca47db542f17cc99;d_ticket=62cff87f3ff289aa7437ccc909334e8ef4ee4;FOLLOW_NUMBER_YELLOW_POINT_INFO=MS4wLjABAAAAKPzirJw1HGVfEwO4NXF6n_SBe0Og9u-bYKFVxzFVHQg%2F1637078400000%2F0%2F1637062705917%2F0;s_v_web_id=verify_kw285zal_5fTNFn8C_S8jN_46E0_AYRu_i7IsGrrtPUjH;MONITOR_WEB_ID=a3f7ce26-6716-4417-8c1b-1733feea0a5b;n_mh=q5_EQDE9aAOu8snC-S8tkXI4H3hDknhfcmYjhD6ViYE;sso_uid_tt=787bc71160e377bf6343e82b768a4f78;sso_uid_tt_ss=787bc71160e377bf6343e82b768a4f78;toutiao_sso_user=fcae792d33a9ac25fcfea9209f2b1629;toutiao_sso_user_ss=fcae792d33a9ac25fcfea9209f2b1629;odin_tt=2439239b99d12ac4e96f073a38189b203807151bf36064976df69cc32b94335a0eca24c3487da8ad1f2ca314cae2481fdcd3129ed328467786b775ec061f49b2;passport_auth_status_ss=521e8181083ccca5f4f5f8d644428e5e%2C27cb9a0918e31b0f2c171e63a68c6f46;sid_guard=9cc14310155f63154c19bf56377482ea%7C1637508768%7C5184000%7CThu%2C+20-Jan-2022+15%3A32%3A48+GMT;uid_tt=6f90575096c31a3263c775d0d76a33c4;uid_tt_ss=6f90575096c31a3263c775d0d76a33c4;sid_tt=9cc14310155f63154c19bf56377482ea;sessionid=9cc14310155f63154c19bf56377482ea;sessionid_ss=9cc14310155f63154c19bf56377482ea;sid_ucp_v1=1.0.0-KDBkNzlmNzA5NmRmOWUwNWNiODM3MTg4ZWFjMDQ5NTM4OTliMmZhNDgKFQiT3PiWgQMQoM3pjAYYlk44BkD0BxoCbHEiIDljYzE0MzEwMTU1ZjYzMTU0YzE5YmY1NjM3NzQ4MmVh;ssid_ucp_v1=1.0.0-KDBkNzlmNzA5NmRmOWUwNWNiODM3MTg4ZWFjMDQ5NTM4OTliMmZhNDgKFQiT3PiWgQMQoM3pjAYYlk44BkD0BxoCbHEiIDljYzE0MzEwMTU1ZjYzMTU0YzE5YmY1NjM3NzQ4MmVh;passport_auth_status=521e8181083ccca5f4f5f8d644428e5e%2C27cb9a0918e31b0f2c171e63a68c6f46;msToken=07kqR5HDcD2pr9bn_ryTmUsRhlxe-hPZOcyPgdGgdq26WdibS9fpfW0dGvwJLHZk1_BeZVHuk9EnBfQzWsMUvu7Ije5lAbSqyGv4Sgqgmo22EllBONC8zA==; ");
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
            osw.write(sb.toString());
            osw.flush();
            osw.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                con.disconnect();
            }
        }

        // 读取返回内容
        StringBuffer buffer = new StringBuffer();
        try {
            //一定要有返回值，否则无法把请求发送给server端。
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
            String temp;
            while ((temp = br.readLine()) != null) {
                buffer.append(temp);
                buffer.append("\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return buffer.toString();
    }
}
